package com.hdax.dm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdax.dm.dto.DescItemDto;
import com.hdax.dm.dto.ItemCinemaDto;
import com.hdax.dm.entity.base.Image;
import com.hdax.dm.entity.item.DmCinema;
import com.hdax.dm.entity.item.DmItem;
import com.hdax.dm.feign.ImageFeign;
import com.hdax.dm.mappers.DescMapper;
import com.hdax.dm.mapstrucs.DescItemDtoMapper;
import com.hdax.dm.mapstrucs.ItemCinemaDtoMapper;
import com.hdax.dm.service.CinemaService;
import com.hdax.dm.service.DescService;
import com.hdax.dm.utils.response.CommonResponse;
import com.hdax.dm.utils.response.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DescSImpl extends ServiceImpl<DescMapper, DmItem> implements DescService {


    @Autowired
    private DescItemDtoMapper descItemDtoMapper;

    @Autowired
    private ItemCinemaDtoMapper itemCinemaDtoMapper;

    @Autowired
    private CinemaService cinemaService;


    @Autowired
    private  ImageFeign imageFeign;

    public  String ggImageFeign(Long targetId,Long type,Long category){
        //通过openfeign远程调用了base，查询图片信息
        Image image=imageFeign.image(targetId, type, category);
        String imageUrl=image==null?"": image.getImgUrl();
        return imageUrl;
    }

    @Override
    public CommonResponse<DescItemDto> getItems(Integer id) {
        QueryWrapper<DmItem> wrapper=new QueryWrapper<>();
        wrapper.eq("id",id);
        DmItem item= getOne(wrapper);
        DmCinema cinema= cinemaService.cinemaOne(item.getCinemaId());
        DescItemDto descItemDto=  descItemDtoMapper.toTarget(item);
        descItemDto.setAreaId(cinema.getAreaId());
        descItemDto.setAreaName(cinema.getAreaName());
        descItemDto.setAdress(cinema.getAddress());
        descItemDto.setImgUrl(ggImageFeign(descItemDto.getId(),2L,1L));
        return ResponseUtil.returnSuccess(descItemDto);
    }

    @Override
    public CommonResponse<List<ItemCinemaDto>> getRecommend(Integer itemTypeId) {
        QueryWrapper<DmItem> wrapper=new QueryWrapper<>();
        wrapper.eq("itemType2Id",itemTypeId);
        wrapper.eq("isRecommend",1);
        wrapper.last("limit 6");
        List<ItemCinemaDto> cinemaDto= itemCinemaDtoMapper.toTargets(list(wrapper));
        cinemaDto.forEach(c->{
           c.setImgUrl(ggImageFeign(c.getId(),2L,1L));
        });
        return ResponseUtil.returnSuccess(cinemaDto);
    }
}
